﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace PuttySessionTool
{
    class CPuttySession
    {
        public const string DefaultSessionKey = "Default%20Settings";
        public const string DefaultRegistryPosition = "Software\\SimonTatham\\PuTTY\\Sessions";
        public static CPuttySession DefaultSession = null;

        public string sSessionKey;
        public string sHostName;
        public UInt32 nPort;

        public CPuttySession(string sSessionKey, string sHostName, UInt32 nPort)
        {
            this.sSessionKey = sSessionKey;
            this.sHostName = sHostName;
            this.nPort = nPort;
        }

        public CPuttySession()
        {

        }

        public static CPuttySession[] GetInstancesFromRegistry()
        {
            RegistryKey rkCurrentUser = Registry.CurrentUser;

            RegistryKey rkPuttySessions = rkCurrentUser.OpenSubKey(DefaultRegistryPosition);
            List<CPuttySession> sessions = new List<CPuttySession>();
            if (rkPuttySessions != null)
            {
                string[] sessionKeys = rkPuttySessions.GetSubKeyNames();
                for (int i = 0; i < sessionKeys.Length; i++)
                {
                    RegistryKey sk = rkPuttySessions.OpenSubKey(sessionKeys[i]);
                    if (sk != null)
                    {
                        string hostname = (string)sk.GetValue("HostName");
                        int port = (int)sk.GetValue("PortNumber");
                        sessions.Add(new CPuttySession(sessionKeys[i], hostname,(UInt32) port));
                        sk.Close();
                    }
                }
                rkPuttySessions.Close();
            }
            return sessions.Count==0 ? null :sessions.ToArray();
        }

        public static CPuttySession GetInstanceBySessionKey(string SessionKey)
        {
            RegistryKey rkCurrentUser = Registry.CurrentUser;

            CPuttySession sessions = null;
            RegistryKey sk = rkCurrentUser.OpenSubKey(DefaultRegistryPosition+"\\" + SessionKey);
            if (sk != null)
            {
                string hostname = (string)sk.GetValue("HostName");
                UInt32 port = (UInt32)sk.GetValue("PortNumber");
                sessions = new CPuttySession(SessionKey, hostname, port);
                sk.Close();
            }
            return sessions;
        }

        public static CPuttySession GetInstanceDefaultSession()
        {
            if (DefaultSession == null)
                return GetInstanceBySessionKey(DefaultSessionKey);
            else
                return DefaultSession;
        }

        public void WriteToRegistry()
        {
            RegistryKey rkCurrentUser = Registry.CurrentUser;

            RegistryKey rkPuttySessions = rkCurrentUser.CreateSubKey(DefaultRegistryPosition+"\\"+this.sSessionKey);
            if (rkPuttySessions != null)
            {
                rkPuttySessions.SetValue("HostName", this.sHostName, RegistryValueKind.String);
                rkPuttySessions.SetValue("PortNumber", this.nPort, RegistryValueKind.DWord);
                rkPuttySessions.Close();
            }

        }

    }
}
